<?php

namespace app\web\controller;

use app\common\controller\Api;
use think\Db;
use Carbon\Carbon;
use app\api\validate\User as UserValidate;

/**
 * 员工控制器
 * @package app\index\controller
 */
class Users extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取会员列表
     */
    public function lists(){
        if ($this->request->isPost()) {
            $limit = $this->request->post('limit');
            $getData = $this->request->post();
            if(!isset($limit) || empty($limit)){
                $limit = '10';
            }
            //楼面端用户
            if($getData['type'] == '1'){
                $data = Db::name('qt_users')
                    ->field('u.id,u.real_name,u.avatar,u.phone,r.name as role_name')
                    ->alias('u')
                    ->leftJoin('qt_user_role r','u.role = r.id')
                    ->where(['store_id'=>$this->userInfo['store_id']])
                    ->whereNotIn('role',['3'])
                    ->paginate($limit)->each(function ($item){
                        $item['avatar'] = get_img_path($item['avatar']);
                        return $item;
                    });
            }
            //技师
            if($getData['type'] == '2'){
                $data = Db::name('qt_users')
                    ->field('u.id,u.real_name,u.avatar,u.phone,r.name as role_name')
                    ->alias('u')
                    ->leftJoin('qt_user_role r','u.role = r.id')
                    ->where(['store_id'=>$this->userInfo['store_id'],'role'=>'3'])
                    ->paginate($limit)->each(function ($item){
                        $item['avatar'] = get_img_path($item['avatar']);
                        return $item;
                    });
            }
            $this->result('获取成功',$data,200);
        }else{
            $this->result('请求错误','',204);
        }
    }

    /**
     * 用户信息详情
     */
    public function detail(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $data = Db::name('qt_users')
                ->alias('u')
                ->leftJoin('qt_user_role r','r.id = u.role')
                ->leftJoin('qt_work_type t','u.type_id = t.id')
                ->field('u.id,u.phone,u.work_number,u.sex,u.real_name,u.work_time,u.card,r.name,t.title,u.status,u.operation')
                ->where('u.id',$getData['user_id'])
                ->find();
            $this->result('获取成功',$data,200);
        }
    }


    /**
     * 用户信息详情
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new UserValidate();
            if (!$validate->scene('add_save')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $update = Db::name('qt_users')->where('id',$getData['user_id'])->update($getData);
            if($update !== false){
                $this->result('更新成功','',200);
            }
            $this->result('更新失败','',204);
        }
        $this->result('无效的请求','',204);
    }


    /**
     * 一件启用用户
     */
    public function enable(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['ids'])){
                $this->result('缺少参数','',204);
            }
            $userIds = explode(',',$getData['ids']);
            foreach ($userIds as $key=>$value){
                Db::name('qt_users')->where('id',$value)->update(['status'=>'0']);
            }
            $this->result('启用成功','',200);
        }
        $this->result('请求错误','',204);
    }


    /**
     * 一件禁用用户
     */
    public function disable(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['ids'])){
                $this->result('缺少参数','',204);
            }
            $userIds = explode(',',$getData['ids']);
            foreach ($userIds as $key=>$value){
                Db::name('qt_users')->where('id',$value)->update(['status'=>'1']);
            }
            $this->result('启用成功','',200);
        }
        $this->result('请求错误','',204);
    }

}
